package com.zy1.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Random;

public class UserService {
    private static final Logger logger = LoggerFactory.getLogger(UserService.class);
    private final Random random = new Random();

    public void processUser(String username) {
        logger.info("开始处理用户：{}", username);

        if (username.isBlank()) {
            logger.warn("用户名为空，跳过处理");
            return;
        }

        try {
            int delay = random.nextInt(1000);
            logger.debug("模拟处理耗时：{}ms", delay);
            Thread.sleep(delay);

            if (username.equalsIgnoreCase("error")) {
                throw new RuntimeException("测试异常：用户名不能为 error");
            }

            logger.info("用户 {} 处理完成 ✅", username);
        } catch (InterruptedException e) {
            logger.error("线程被中断", e);
            Thread.currentThread().interrupt();
        }
    }
}
